import jsonpath
import requests

from 接口自动化.interface_auto.apis.wework import WeWork


class Department(WeWork):
    def __init__(self):
        super().__init__()
        secret = self.data.get("secret", {}).get("contacts")
        self.token = self.get_token(secret)
        self.create_url = f"https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token={self.token}"
        self.delete_url = f"https://qyapi.weixin.qq.com/cgi-bin/department/delete?access_token={self.token}"
        self.list_url = f"https://qyapi.weixin.qq.com/cgi-bin/department/simplelist?access_token={self.token}"
        self.update_url = f"https://qyapi.weixin.qq.com/cgi-bin/department/update?access_token={self.token}"

    def create_department(self, data):
        """
        创建部门
        :return:
        """
        return self.send("POST", self.create_url, json=data)

    def update_department(self, data):
        """
        更新部门
        :return:
        """
        return self.send("POST", self.update_url, json=data)

    def delete_department(self, _id):
        """
        删除部门
        :return:
        """
        return self.send("GET", self.delete_url, params={"id": _id})

    def list_department(self):
        """
        部门列表
        :return:
        """
        return self.send("GET", self.list_url)

    def clear_department(self):
        """
        清除数据
        :return:
        """
        depart_list = self.list_department()
        depart_id_list = jsonpath(depart_list.json(), "$..id")
        for i in depart_id_list:
            if i !=1:
                self.delete_department(i)
